/*
* Copyright (c) 2009, Active Web Solutions Limited
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3- The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY Active Web Solutions Limited ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Active Web Solutions Limited BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.aws.jazure;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.aws.jazure.httpconstants.XMLElements;

import org.restlet.engine.util.Base64;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Objects of this class represent a single message in the queue.
 * 
 * @author dyaa.albakour
 */
public class QueueMessage {
	
	/**
	 * Tha maximum message size in byte.
	 */
	public static int MaxMessageSize= 8* 1024;	
	/**
	 * The maximum amount of time a message is kept in the queue. Maximum value is 7 days.
	 * Value is given in seconfds.
	 */
	public static int MaxTimeToLive = 7* 24* 60 * 60;
	
	private String messageId;
	private Calendar insertionTime;
	private Calendar expirationTime;
	private String popReceipt;
	private String timeNextVisible;
	private byte[] content;
	
	
	// Add constructor from the XML response?
	
	public QueueMessage() {
		
	}
	
	public QueueMessage(byte[] content){
		 
	}
	
	public QueueMessage(String text){
		setContent(text);
	}


	public String toString(){
		
		StringBuilder builder = new StringBuilder();
		builder.append("Queue Message\n");
		builder.append("Message ID: " + messageId+"\n");
		builder.append("Message Conent: "+ new String(content)+"\n");
		
		return builder.toString();
	}
	
	

	public String getMessageId() 
	{
		return this.messageId;
	}
	
	public Calendar getInsertionTime() 
	{
		return this.insertionTime;
	}
	
	public Calendar getExpirationTime() 
	{
		return this.expirationTime;
	}
	
	public String getPopReceipt() 
	{
		return this.popReceipt;
	}
	
	public String getTimeNextVisible() 
	{
		return this.timeNextVisible;
	}
	
	/**
	 * Sets the content of the QueueMessage with the given textual representation
	 * 
	 * @param text
	 */
	public void setContent(String text){
		// TODO check message length		
		try {
			this.content = text.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {			
		}
	}
	
	public void setCotent(byte[] content){
		//TODO check message length
		this.content = content ;
	}
	

	void setMessageId(String messageId) {
		this.messageId = messageId;
	}
	
	void setInsertionTime(Calendar insertionTime) {
		this.insertionTime = insertionTime;
	}
	
	void setExpirationTime(Calendar expirationTime) {
		this.expirationTime = expirationTime;
	}
	
	void setPopReceipt(String popReceipt) {
		this.popReceipt = popReceipt;
	}
	
	void setTimeNextVisible(String timeNextVisible) {
		this.timeNextVisible = timeNextVisible;
	}
	
	/**
	 * Get an XML representationof the Queue message as a DOM document.
	 * 
	 * @return A DOM document representing the message
	 * @throws ParserConfigurationException 
	 */
	public Document getXMLRepresentation() 
		throws ParserConfigurationException{
		Document messageDoc=null;
		
		messageDoc = 
			DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		Element queueMessageElement = messageDoc.createElement(XMLElements.QueueMessage);
		Element messageTextElement = messageDoc.createElement(XMLElements.MessageText);
		
		messageTextElement.appendChild(messageDoc.createTextNode(Base64.encode(content,true)));			
		queueMessageElement.appendChild(messageTextElement);
		messageDoc.appendChild(queueMessageElement);			
			
		
		return messageDoc;
		
	}
}
